﻿<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="loginTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Log On
</asp:Content>

<asp:Content ID="loginContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Log On</h2>
    <p>
        Please enter your username and password.
    </p>
    <p id="error" style="display:none;color:Red;"></p>
    <%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %>

    <% using (Html.BeginForm()) { %>
        <div>
            <fieldset>
                <legend>Account Information</legend>
                <p>
                    <label for="username">Username:</label>
                    <%= Html.TextBox("username") %>
                    <%= Html.ValidationMessage("username") %>
                </p>
                <p>
                    <label for="password">Password:</label>
                    <%= Html.Password("password") %>
                    <%= Html.ValidationMessage("password") %>
                </p>
                <p>
                    <%= Html.CheckBox("rememberMe") %> <label class="inline" for="rememberMe">Remember me?</label>
                </p>
                <p>
                    <input id="logon" type="button" value="Log On" />
                </p>
            </fieldset>
        </div>
    <% } %>
    
    <script type="text/javascript">
    	$(function() {
	    	$("#logon").click(function() {
    			$("#error").text('').hide();
    			$.get('http://localhost:21259/SSOService.svc/user/Login?callback=?',
    				{ username: $("#username").val(), password: $("#password").val() },
    				function(ssodata) {
    				if (ssodata.LoginResult.Status == 'DENIED') {
    					$("#error").text('Login Failed').show();
    				} else {
    					// redirect to authentication page instead of duplicating code here
    					document.location = '<%=Url.Action("Authenticate", "Account") %>';
    				}
    				// make sure to tell jQuery this is a JSONP call
    			}, 'jsonp');
    		});
    	});
    </script>
</asp:Content>
